package com.next.aappublicapp;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.next.aap.client.AssemblyConstituencyWithManifesto;
import com.next.aap.client.BaseItem;
import com.next.aappublicapp.adapters.StateConstituencyManifestoListAdapter;
import com.next.aappublicapp.server.service.DataServices;
import com.next.aappublicapp.tasks.AapAsyncTask;
import com.next.aappublicapp.util.TrackerUtl;

public class StateConstituencyManifestoActivity extends BaseListActivty {

	LinearLayout loadingStatusLayout;
	ListView list;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_state_constituency_manifesto);
		
		loadingStatusLayout = (LinearLayout)findViewById(R.id.loading_status);
		list = (ListView)findViewById(android.R.id.list);
		
		loadAssemblyConstituencyAsync();

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.state_constituency_manifesto, menu);
		return true;
	}

	private void loadAssemblyConstituencyAsync(){
		new AapAsyncTask<String, String, List<AssemblyConstituencyWithManifesto>>("","") {
			@Override
			protected void beforeExecute(){
				loadingStatusLayout.setVisibility(View.VISIBLE);
				list.setVisibility(View.GONE);
			}
			
			@Override
			protected List<AssemblyConstituencyWithManifesto> doInBackground(
					String... params) {
				List<AssemblyConstituencyWithManifesto> allAssemblies = null;
				try{
					allAssemblies = DataServices.getInstance().getAssemblyConstituencyOfDelhiForManifesto();
					return allAssemblies;	
				}catch(Exception ex){
					ex.printStackTrace();
				}
				allAssemblies = new ArrayList<AssemblyConstituencyWithManifesto>();
				return allAssemblies;
				
			}
			@Override
			protected void afterExecute(List<AssemblyConstituencyWithManifesto> result){
				loadingStatusLayout.setVisibility(View.GONE);
				list.setVisibility(View.VISIBLE);
				StateConstituencyManifestoListAdapter adapter = new StateConstituencyManifestoListAdapter(StateConstituencyManifestoActivity.this, result);
				setListAdapter(adapter);
			}
		}.execute();
	}
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
 
		//get selected items
		//String selectedValue = ((AssemblyConstituencyDto) getListAdapter().getItem(position)).getName();
		AssemblyConstituencyWithManifesto selectedAssemblyConstituencyWithManifesto = ((AssemblyConstituencyWithManifesto) getListAdapter().getItem(position));
		if(selectedAssemblyConstituencyWithManifesto.isAvailable()){
			TrackerUtl.sendAapEvent("Manifesto");
			Intent intent = new Intent(this, ManifestoActivity.class);
			Bundle bundle = new Bundle();
			bundle.putSerializable(ManifestoActivity.ARGS_AC, selectedAssemblyConstituencyWithManifesto);
			intent.putExtra(ManifestoActivity.ARGS_BUNDLE, bundle);
			this.startActivity(intent);
			
		}else{
			Toast.makeText(this, "Manifesto is still in drafting stage, will be available shortly.", Toast.LENGTH_SHORT).show();
			TrackerUtl.sendAapEvent("ManifestoNotAvailable");
		}
	}
}
